import { createRouter, createWebHistory } from 'vue-router'
import LayOut from '@/views/layout/index.vue'
import Login from '@/views/login/index.vue'
import { useUserStore } from '@/stores/modules/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'layout',
      redirect: '/book',
      component: LayOut,
      children: [
        {
          path: '/book',
          name: 'book',
          component: () => import('@/views/bookPage/index.vue')
        },
        {
          path: '/system',
          name: 'system',
          component: () => import('@/views/systemPage/index.vue')
        },
        {
          path: '/order',
          name: 'order',
          component: () => import('@/views/orderPage/index.vue')
        },
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
    },
  ],
})

// 登录访问拦截
router.beforeEach((to, from) => {
  const userStore = useUserStore()
  if (!userStore.userInfo.token && to.path !== '/login') return '/login'
})

export default router
